<template>
  <div id="empAdd">
    <div class="addTable">
      <!-- 用户名 -->
      <div class="username">
        <span class="essential">*</span>
        <span>用户名</span>
        <el-input
          v-model="newWorker.username"
          placeholder="请输入内容"
        ></el-input>
      </div>
      <!-- 密码 -->
      <div class="password">
        <span class="essential">*</span>
        <span>密码</span>
        <el-input
          v-model="newWorker.password"
          placeholder="请输入内容"
        ></el-input>
      </div>
      <!-- 昵称 -->
      <div class="nickname">
        <span class="essential">*</span>
        <span>昵称</span>
        <el-input
          v-model="newWorker.nickname"
          placeholder="请输入内容"
        ></el-input>
      </div>
      <!-- 性别 -->
      <div class="gender">
        <span class="essential">*</span>
        <span>性别</span>
        <el-radio v-model="newWorker.gender" label="男">男</el-radio>
        <el-radio v-model="newWorker.gender" label="女">女</el-radio>
      </div>
      <!-- 联系电话 -->
      <div class="phone">
        <span class="essential">*</span>
        <span>联系电话</span>
        <el-input v-model="newWorker.phone" placeholder="请输入内容"></el-input>
      </div>
      <!-- 入职部门 -->
      <div class="department">
        <span class="essential">*</span>
        <span>入职部门</span>
        <el-radio v-model="newWorker.department" label="客服部"
          >客服部</el-radio
        >
        <el-radio v-model="newWorker.department" label="运营部"
          >运营部</el-radio
        >
        <el-radio v-model="newWorker.department" label="宣传部"
          >宣传部</el-radio
        >
        <el-radio v-model="newWorker.department" label="策划部"
          >策划部</el-radio
        >
        <el-radio v-model="newWorker.department" label="人事部"
          >人事部</el-radio
        >
        <el-radio v-model="newWorker.department" label="监督部"
          >监督部</el-radio
        >
      </div>
      <!-- 员工头像 -->
      <div class="avatar1">
        <span>员工头像</span>
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8888/api/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="newWorker.avatar" :src="newWorker.avatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      <!-- 提交按钮 -->
      <div class="submit">
        <el-button @click="workerAdd()" type="primary" plain
          >立即创建</el-button
        >
        <el-button @click="reset()" plain>重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { addWorker } from "@/api/admin.js";
export default {
  name: "empAdd",
  data() {
    return {
      newWorker: {
        username: "",
        password: "",
        nickname: "",
        gender: "",
        phone: "",
        avatar: "",
        entry: "",
        department: "",
      },
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.newWorker.avatar = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 10;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 10MB!");
      }
      return isJPG && isLt2M;
    },
    // 重置按钮
    reset: function () {
      this.newWorker.username = "";
      this.newWorker.password = "";
      this.newWorker.nickname = "";
      this.newWorker.gender = "";
      this.newWorker.phone = "";
      this.newWorker.avatar = "";
      this.newWorker.entry = "";
      this.newWorker.department = "";
    },
    workerAdd: function () {
      addWorker(
        this.newWorker.username,
        this.newWorker.password,
        this.newWorker.nickname,
        this.newWorker.gender,
        this.newWorker.phone,
        this.newWorker.avatar,
        1,
        this.newWorker.department
      ).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: "添加成功",
            type: "success",
          });
        } else {
          this.$message({
            message: "添加失败",
            type: "error",
          });
        }
      });
    },
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
#empAdd {
  .addTable {
    display: flex;
    flex-direction: column;
    margin: 20px 0 0 40px;
    .essential {
      color: #d9001b;
    }
    .username,
    .password,
    .nickname,
    .gender,
    .phone,
    .department {
      margin-top: 20px;
      display: flex;
      flex-direction: row;
      align-items: center;
    }
    // 用户名
    .username {
      .el-input {
        margin-left: 25px;
      }
    }
    // 密码
    .password {
      .el-input {
        margin-left: 40px;
      }
    }
    // 昵称
    .nickname {
      .el-input {
        margin-left: 40px;
      }
    }
    // 性别
    .gender {
      span:nth-child(2) {
        margin-right: 45px;
      }
    }
    // 联系电话
    .phone {
      .el-input {
        margin-left: 10px;
      }
    }
    // 入职部门
    .department {
      span:nth-child(2) {
        margin-right: 15px;
      }
    }
    // 头像
    .avatar1 {
      display: flex;
      flex-direction: row;
      margin-top: 20px;
      .avatar-uploader .el-upload {
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
      .avatar-uploader .el-upload:hover {
        border-color: #409eff;
      }
      .avatar-uploader-icon {
        border-radius: 6px;
        margin-left: 20px;
        border: 1px dashed #d9d9d9;
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
      }
      .avatar {
        width: 178px;
        height: 178px;
        display: block;
      }
    }
    // 提交按钮
    .submit {
      margin: 40px 0 0 0px;
    }
    .el-input {
      width: 320px;
    }
  }
}
</style>